package com.tubeonfire.controller;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tubeonfire.entity.Category;
import com.tubeonfire.entity.Tube;
import com.tubeonfire.model.CategoryModel;
import com.tubeonfire.util.JavaCacheHandle;

public class CategoryController extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private static int itemPerPage = 12;

	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		response.setHeader("Cache-Control", "public, max-age=86400");
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE, 1);
		response.setHeader("Expires", cal.getTime().toString());
		// search by input key.
		try {
			String path = ((HttpServletRequest) request).getRequestURI();
			String[] splittedURI = path.split("/");
			int page = 1;
			String cateString = "";
			if (splittedURI.length == 3) {
				cateString = splittedURI[splittedURI.length - 1];

			} else if (splittedURI.length == 4) {
				cateString = splittedURI[splittedURI.length - 2];
				try {
					System.out.println(splittedURI[splittedURI.length - 1]);
					page = Integer
							.parseInt(splittedURI[splittedURI.length - 1]);
				} catch (Exception e) {
					System.out.println("Error when parse page string to int.");
					page = 1;
				}
			} else {
				System.out.println("Error path.");
				response.sendRedirect("/home");
			}
			cateString = URLDecoder.decode(cateString, "UTF8");
			System.out.println("Cate to search : " + cateString);
			Category cate = CategoryModel.getByCateAlias(cateString);
			if (cate != null) {
				List<Tube> result = new ArrayList<Tube>();
				int startPoint = (page - 1) * itemPerPage;
				if (cate.getListTubes().size() > startPoint) {
					System.out.println("Ok here.");
					for (int i = startPoint; i < cate.getListTubes().size(); i++) {
						if (i == (itemPerPage + startPoint)) {
							break;
						}

						Tube tub = new Tube();
						tub.transformString(cate.getListTubes().get(i)
								.getValue());
						result.add(tub);
					}
				} else {
					page = 1;
					for (int i = 0; i < cate.getListTubes().size(); i++) {
						if (i == itemPerPage) {
							break;
						}
						Tube tub = new Tube();
						tub.transformString(cate.getListTubes().get(i)
								.getValue());
						result.add(tub);
					}
				}

				request.setAttribute("url", request.getRequestURL());
				request.setAttribute("listCategory",
						JavaCacheHandle.getCategory());
				request.setAttribute("listChannel",
						JavaCacheHandle.getChannels());
				request.setAttribute("result", result);
				request.setAttribute("currentPage", page);
				request.setAttribute("totalResult", cate.getListTubes().size());
				request.setAttribute("cate", cateString);
				request.setAttribute("cateString",
						cateString.replaceAll("\\+", " "));
				request.getRequestDispatcher("/category_show.jsp").forward(
						request, response);
			} else {
				System.out.println("Error path.");
				response.sendRedirect("/home");
			}
		} catch (Exception e) {
			System.out.println("Exception.");
			e.printStackTrace();
			response.sendRedirect("/home");
		}
	}

}
